import db from "../models/index.js";
import {getNow} from "../utils/utils.js";
import {Op} from "sequelize";
import userBaseDao from "./userbaseDB.js";

let groupBaseInfo = db.group_base_info;
let groupBaseInfoDao = {};
//判断获取是否存在这个群聊
groupBaseInfoDao.isExistedByAccount = (_group_account) => {
  return groupBaseInfo.findOne({
    where: {
      _group_account
    },
    raw: true,
  });
};
//获取群组数据--数组
groupBaseInfoDao.getGroupBaseInfoList = (_groupAccountList, startTime = 0) => {
  return groupBaseInfo.findAll({
    where: {
      _group_account: {
        [Op.in]: _groupAccountList,
      },
      _update_time: {
        [Op.gte]: startTime
      }
    }
  });
};
// 判断是否是群主
groupBaseInfoDao.isLeaderByAccount = (_leader, _group_account) => {
  return groupBaseInfo.findOne({
    where: {
      _group_account,
      _leader
    },
    raw: true,
  });
};
// 判断是否存在群
groupBaseInfoDao.isExistedBySessionID = (_session_id) => {
  return groupBaseInfo.findOne({
    where: {
      _session_id
    },
    raw: true,
  });
};
// 判断是否是群主
groupBaseInfoDao.isLeaderBySessionID = (_leader, _session_id) => {
  return groupBaseInfo.findOne({
    where: {
      _session_id,
      _leader,
    },
    raw: true,
  });
};
//添加群聊基础信息 创建新群
groupBaseInfoDao.addGroupBaseInfo = (_group_account, _session_id, _name, _avatar, _birth, _leader, t) => {
  let _update_time = getNow();
  if (!t) {
    return groupBaseInfo.create({_group_account, _session_id, _name, _avatar, _birth, _leader, _update_time});
  }
  return groupBaseInfo.create({_group_account, _session_id, _name, _avatar, _birth, _leader, _update_time}, {transaction: t});
};
//删除群聊基础信息 约等于解散群
groupBaseInfoDao.deleteGroupBaseInfo = (_group_account, t) => {
  if (!t) {
    return groupBaseInfo.destroy({where: {_group_account}});
  }
  return groupBaseInfo.destroy({where: {_group_account}}, {transaction: t});
};
//修改群聊的基础信息
groupBaseInfoDao.updateGroupBaseInfo = (_group_account, _session_id, _name, _avatar, _birth, _leader, t) => {
  let _update_time = getNow();
  if (!t) {
    return groupBaseInfo.update({_group_account, _session_id, _name, _avatar, _birth, _leader, _update_time}, {
      where: {
        _group_account,
        _session_id
      }
    });
  }
  return groupBaseInfo.update({_group_account, _session_id, _name, _avatar, _birth, _leader, _update_time}, {
    where: {
      _group_account,
      _session_id
    }, transaction: t
  });
};
export default groupBaseInfoDao;
